﻿using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Application.Components.Platform
{

    public interface IPosMemberPromotionRuleService : IApplicationService
    {
        Task<PosMemberPromotionRule[]> AttachWarehouse(PosMemberPromotionRule[] data);
    }

    public class PosMemberPromotionRuleService : MyApplicationService, IPosMemberPromotionRuleService
    {
        private readonly IRepository<PosMemberPromotionRule> _posMemberPromotionRule;

        public PosMemberPromotionRuleService(IRepository<PosMemberPromotionRule> posMemberPromotionRule)
        {
            this._posMemberPromotionRule = posMemberPromotionRule;
        }

        public async Task<PosMemberPromotionRule[]> AttachWarehouse(PosMemberPromotionRule[] data)
        {
            var db = await this._posMemberPromotionRule.GetDbContextAsync();

            await db.AttachData<Warehouse.Entity.Warehouse, PosMemberPromotionRule>(
                data, idGetter: x => x.warehouseID, action: (m, arr) =>
                {
                    m.Warehouse = arr.FirstOrDefault();
                });

            return data;
        }
    }
}
